<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="800" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" :disabled="mode == 'show'" ref="dialogForm" label-width="110px" label-position="right">
            <el-row :gutter="1">
                <el-col :span="12">
                    <el-form-item label="公司名称" prop="company_name">
                        <el-input v-model="form.company_name" placeholder="请输入公司名称" clearable></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="对接人" prop="contacts">
                        <el-input v-model="form.contacts" placeholder="请输入对接人" clearable></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row :gutter="1">
                <el-col :span="12">
                    <el-form-item label="手机号码" prop="mobile">
                        <el-input v-model="form.mobile" placeholder="请输入手机号码" clearable></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="客资来源" prop="hotel">
                        <el-select v-model="form.hotel" placeholder="" style="width:100%" clearable filterable>
                            <el-option v-for="item in hotelList" :key="item.id" :label="item.hotel_name" :value="item.id"/>
                        </el-select>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-col :span="12">
                <el-form-item label="销售" prop="salesman">
                    <el-select v-model="form.salesman" filterable clearable placeholder="请选择销售" style="width:100%">
                        <el-option v-for="item in saletList" :key="item.id" :label="item.name" :value="item.id"/>
                    </el-select>
                </el-form-item>
            </el-col>
		</el-form>
		<template #footer>
			<el-button @click="visible = false">取 消</el-button>
			<el-button v-if="mode != 'show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>
export default {
	emits: ['success', 'closed'],
	data() {
		return {
			mode: "add",
			titleMap: {
				add: '新增客户',
				edit: '编辑客户',
				show: '查看'
			},
			visible: false,
			isSaveing: false,
			//表单数据
			form: {
                id:"",
				company_name: "",
				contacts: "",
				mobile: "",
				hotel: "",
				salesman: "",
                apply_code: "",
			},
			//验证规则
			rules: {
				company_name: [{ required: true, message: '请输入公司名称' }],
			},
            depts: [],
			deptsProps: {
				value: "id",
				checkStrictly: true
			},
            saletList:[],
            hotelList:[],
		}
	},
	mounted() {
        this.getUser()
        this.getHotel()
	},
	methods: {
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		//表单提交方法
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					var res = [];
					if (this.form.id) {
						res = await this.$API.client.client.edit.post(this.form);
					} else {
						res = await this.$API.client.client.add.post(this.form);
					}
					this.isSaveing = false;
					if (res.code == 1) {
						this.$emit('success', this.form, this.mode)
						this.visible = false;
						this.$message.success("操作成功")
					} else {
						this.$alert(res.message, "提示", { type: 'error' })
					}
				} else {
					return false;
				}
			})
		},
		//表单注入数据
		setData(data) {
			//可以和上面一样单个注入，也可以像下面一样直接合并进去
            this.form.id = data.id
            this.form.apply_code = data.apply_code
            this.form.company_name = data.company_name
            this.form.contacts = data.contacts
            this.form.mobile = data.mobile
            this.form.hotel = data.hotel.id
            this.form.salesman = Number(data.salesman)
		},
        // 获取行业
        async getDept() {
			var res = await this.$API.system.dept.list.get();
			this.depts = res.data;
		},
        // 销售
        async getUser(){
            var res = await this.$API.system.admin.all.get({joball:1});
            this.saletList = res.data;
        },
        async getHotel(){
            var res = await this.$API.client.hotel.list.get();
            this.hotelList = res.data.rows;
        },
        change(e){
            console.log(e);
            if(e==13){
                this.show = true
            }
        },
	}
}
</script>

<style></style>
